<template>
  <div onscroll="countScroll" class="originNumberClass">
    <button :disabled="isAnimating" @click="subtract">
      <!-- <i class="fas fa-minus"></i> -->
      -
    </button>
    <span :class="{ before: isBefore, after: isAfter }" :data-before="countBefore" :data-after="countAfter">
      {{ count }}
    </span>
    <button :disabled="isAnimating" @click="add">
      <!-- <i class="fas fa-plus"></i> -->
      +
    </button>
  </div>
</template>

<script>
export default {
  // OriginNumber 原生计数器
  name: 'OriginNumber',

  components: {},

  props: {},

  data() {
    return {
      count: 10,
      countBefore: 9,
      countAfter: 11,
      isBefore: false,
      isAfter: false
    }
  },

  computed: {
    isAnimating() {
      return this.isBefore || this.isAfter
    }
  },

  watch: {},

  created() {},

  mounted() {},

  methods: {
    subtract() {
      this.isBefore = true

      setTimeout(() => {
        this.count--
        this.countBefore = this.count - 1
        this.countAfter = this.count + 1
        this.isBefore = false
      }, 200)
    },
    add() {
      this.isAfter = true

      setTimeout(() => {
        this.count++
        this.countBefore = this.count - 1
        this.countAfter = this.count + 1
        this.isAfter = false
      }, 200)
    },
    countScroll() {
      console.log(1)
    }
  }
}
</script>

<style lang="scss" scoped>
// :root {
//   font-size: 100px;
//   font-family: Helvetica;
// }

// body {
//   display: flex;
//   justify-content: center;
//   align-items: center;
//   min-height: 100vh;
// }

.originNumberClass {
  // width: 5rem;
  // height: 1rem;
  width: 10rem;
  height: 2rem;
  background-color: #000;
  border-radius: 0.2rem;
  display: flex;
  overflow: hidden;
  padding: 0.4rem 0;
  position: relative;
  box-shadow: 0px 3px 12px rgba(0, 0, 0, 0.2);
}

.originNumberClass::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  background: linear-gradient(
    180deg,
    rgba(0, 0, 0, 0.9) 0%,
    rgba(0, 0, 0, 0) 30%,
    rgba(0, 0, 0, 0) 70%,
    rgba(0, 0, 0, 0.9) 100%
  );
  width: 100%;
  height: 100%;
  border-radius: 0.2rem;
}

span {
  color: #fff;
  display: block;
  flex: 1 1 5rem;
  font-size: 1rem;
  // line-height: 1rem;
  line-height: 1rem;
  text-align: center;

  transform: translateY(-1rem);
}

span.before {
  transform: translateY(0rem);
  transition: transform 0.2s ease-in;
}

span.after {
  transform: translateY(-2rem);
  transition: transform 0.2s ease-in;
}

span::before {
  display: block;
  content: attr(data-before);
}

span::after {
  display: block;
  content: attr(data-after);
}

button {
  flex: 0 0 2rem;
  border: 0;
  background: none;
  color: #fff;
  font-size: 1.6rem;
  line-height: 1rem;
  padding: 0;
  margin: 0;
  width: 1rem;
  height: 1rem;
  position: relative;
  z-index: 100;
  outline: none;
}
</style>
